/**
 * OpenGL rendering thread
 * Purpose	Drawing on the screen
 * 			Gets objects from game logic thread to draw
 * 			Gets user touch gesture input
 */
package com.unknown.explorer;

import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.TreeMap;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.threed.jpct.*;
import com.threed.jpct.util.MemoryHelper;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;

public class renderTaskOpenGL implements GLSurfaceView.Renderer{

	private FrameBuffer fb = null;
	private volatile static World world = null;
	private int fps = 0;
	private long time = System.currentTimeMillis();
	private RGBColor back = new RGBColor(50, 50, 100);
	
	private Object3D cube = null;
	private Light sun = null;
	private Object3D plane;
	static Camera cam;
	
	private static TreeMap<Object,Object> my3dObjects;
	private Object3D cameraTarget;
	
	public Handler h_3D;
	
	private float scale = 0.05f;
	private float touchTurn = 0;
	private float touchTurnUp = 0;
	private float xpos = -1;
	private float ypos = -1;
	
	public static void main(String[] args) {
		
	}
	
	public renderTaskOpenGL(Context appContext){
		my3dObjects = new TreeMap<Object, Object>();
		
		//create cubes
	//	Object3D newObj = Primitives.getCube(30);
	//	newObj.build();
	//	my3dObjects.put("cameratarget", newObj);	
	//	setCameraTarget("cameratarget");
		
		h_3D = new Handler_render();

	}
	
/*	public boolean setCameraTarget(String name){
		Object3D target = (Object3D) my3dObjects.get(name);
		if(target == null)
			return false;
		cameraTarget = target;
		return true;
	}
*/	
	public synchronized void onDrawFrame(GL10 gl) {
		
		if(world != null)
		{
			Camera cam = world.getCamera();	
			
			//set camera position and orientation
			cam.setPosition(0, -50, -200);
			SimpleVector look = new SimpleVector(0,0,0);
			cam.lookAt(look);
			
			//clean up objects
			world.removeAllObjects();
			
			//add objects to the world
			Collection<Object> c = my3dObjects.values();
			Iterator<Object> i = c.iterator();
			while(i.hasNext() == true){		
				world.addObject((Object3D) i.next());
			}
	
//			if (touchTurn != 0) {
//				cameraTarget.rotateY(touchTurn);
//				//plane.rotateY(touchTurn);
//				touchTurn = 0;
//			}
//	
//			if (touchTurnUp != 0) {
//				cameraTarget.rotateX(touchTurn);
//				//plane.rotateX(touchTurnUp);
//				touchTurnUp = 0;
//			}
			
			fb.clear(back);
			world.renderScene(fb);
			world.draw(fb);
			fb.display();
			
			if (System.currentTimeMillis() - time >= 1000) {
				Logger.log(fps + "fps");
				fps = 0;
				time = System.currentTimeMillis();
			}
			fps++;
		}
	}

	public void onSurfaceChanged(GL10 gl, int w, int h) {
		if (fb != null) {
			fb.dispose();
		}
		
		fb = new FrameBuffer(gl, w, h);

	//	world = new World();
//		world.setAmbientLight(20, 20, 20);
//
//		sun = new Light(world);
//		sun.setIntensity(250, 250, 250);
//		
//		SimpleVector sv = new SimpleVector();
//		sv.y -= 100;
//		sv.z -= 100;
//		sun.setPosition(sv);

		MemoryHelper.compact();

	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		
	}

	
	
	static class Handler_render extends Handler{
		public synchronized void handleMessage(Message msg){
			//synchronize map
			gameMessage receivedMsg;
			receivedMsg = (gameMessage) msg.obj;
			
			if(receivedMsg.messageType == "Object3D")
			{
				my3dObjects = receivedMsg.myData;
			}
			
			if(receivedMsg.messageType == "setCamera")
			{
				Camera w = (Camera) receivedMsg.myData2;
				if(w != null)
				{
					cam = w;
				}
			}
			
			if(receivedMsg.messageType == "setLight")
			{
				
			}
		}
	}
}
